qui(
    function (require, exports, module) {
        //分片管理
        WrapSys.Fragment = (function () {

            var node = null;

            function createBaseNode(parentDiv, callhandler) {
                return {
                    settings: WrapSys.Dao.settings,
                    paramsData: WrapSys.Dao.getParamsData(),
                    processArr: [doProcess1, doProcess2, doProcess3, doProcess4, doProcess6, doProcess7 , doProcess5],
                    handler: callhandler,
                    parentDiv: parentDiv,
                    tempNode: {}
                }
            }

            function doProcess1() {
                if (node.settings.taskid < 1) {
                    node.handler.doError(WrapSys.Config.REMIND_MSG_INFO.NO_TASK_ID_INFO);
                    return false;
                }
                if ($("span[span-mode=warn]").is(":visible")) {
                    node.handler.doError(WrapSys.Config.REMIND_MSG_INFO.TOO_LONG_TITLE_INFO);
                    return false;
                }

                return true;
            }

            function doProcess2() {
                var sTime = $("#cForm").find("input[name=sTime]").val()
                var eTime = node.parentDiv.find("input:eq(1)").val();
                var pic = sTime;
                if (!WrapSys.Util.checkStAndEt(sTime, eTime)) {
                    node.handler.doError(WrapSys.Config.REMIND_MSG_INFO.ERROR_STIME_INFO, true);
                    return false;
                }
                var checkArr = [];
                var numTime = $('#picNum').text();
                checkArr.push({value: parseInt(eTime), error: WrapSys.Config.REMIND_MSG_INFO.NO_EXIST_END_INFO, type: "lt", other: parseInt(numTime)  })
                if (!WrapSys.Plugin.Validate.batchCheck(checkArr)) {
                    node.handler.doError("interrupt checkLiveType checkRepeat", true);
                    return false;
                }
                if (WrapSys.Advert.checkContains([
                    {point: parseInt(sTime), errorMsg: WrapSys.Config.REMIND_MSG_INFO.ADVERT_CONTAINS_INPOINT_INFO}
                ])) {
                    node.handler.doError("", true);
                    return false;
                }
                var errorDataArr = WrapSys.CornerManager.findNoPositionDatas(sTime, eTime);
                if (errorDataArr.length > 0) {
                    node.handler.doError("[贴片 " + errorDataArr.join(",")+"]" + WrapSys.Config.REMIND_MSG_INFO.NEED_SET_RATIO_AND_POS, false);
                    return false;
                }
                if (!WrapSys.ShimManager.checkRangeHasAllSetFrame(sTime, eTime)) {
                    node.handler.doError(WrapSys.Config.REMIND_MSG_INFO.SHIM_NEED_ACCURATE_FRAME, false);
                    return false;
                }
                node.tempNode.sTime = sTime;
                node.tempNode.eTime = eTime;
                node.tempNode.numTime = numTime;
                node.tempNode.pic = pic;
                return true;
            }

            function doProcess3() {
                var buildNewPA = WrapSys.Util.buildNewPA();
                var data = {
                    '起始帧': node.tempNode.sTime,
                    '结束帧': node.tempNode.eTime,
                    '标题': buildNewPA.title,
                    '频道': buildNewPA.cat
                }
                if (!WrapSys.Util.validAddInfo(data)) {
                    node.handler.doError("validAddInfo 失败", true);
                    return false;
                }
                node.tempNode.buildNewPA = buildNewPA;
                return true;
            }

            function doProcess4() {
                var buildNewPA = node.tempNode.buildNewPA;
                $('input[name=imageId]').val($('input[name=imageId]').val().trim());
                var fantasyTitle = $('input[name=fantasyTitle]').val() ? $('input[name=fantasyTitle]').val() : "";
                var isVolumeStandard = $('input[name=isVolumeStandard]:checked').val(); //音量标准化参数
                var isVolumeStandardForMaterial = $('input[name=isVolumeStandardForMaterial]:checked').val();
                $('#addInfo').attr('disabled', 'disabled');
                // 填充全部 开始结束间所有数据
                VODCom.Player.splitData(node.tempNode.sTime, node.tempNode.eTime, false);
                var p_vid = $('input[name=videoName]').suggest("getValue") || 0;
                var p_albumId = $('input[name=albumName]').suggest("getValue") || "";
                var videotype = $('input[name=videotype]:checked').val();
                if (videotype == -1) {
                    p_vid = 0
                }

                var dataJson = {
                    'videoId': p_vid,
                    'priority': buildNewPA.priority,
                    'albumName': buildNewPA.album,
                    'videoName': buildNewPA.title,
                    'categoryId': buildNewPA.cat,
                    'albumId': p_albumId,
                    'reTranscodeConfirm': buildNewPA.reTranscodeConfirm,
                    'freeType': node.paramsData.freeType,
                    'isVolumeStandard': isVolumeStandard,
                    isVolumeStandardForMaterial: isVolumeStandardForMaterial,
                    userArea: WrapSys.Util.getUserRegion()
                };
                if ($("#locationDiv").is(":visible")) {
                    dataJson.locationCategory = $("input[name=location]").suggest("getValue");
                }
                if (videotype == -1) {
                    dataJson.fantasyTitle = fantasyTitle;
                    buildNewPA.fantasyTitle = fantasyTitle;
                }
                node.tempNode.dataJson = dataJson;
                node.tempNode.p_vid = p_vid;
                return true;
            }

            //final submit
            function doProcess5() {
                var buildNewPA = node.tempNode.buildNewPA;
                var dataJson = node.tempNode.dataJson;
                var p_vid = node.tempNode.p_vid;
                var dealRoute = -1;
                var wrapDelayRoute = false;
                if (WrapSys.Config.lastLegoSubmitObj.programId != WrapSys.Config.fragmentPid) {
                    WrapSys.Config.lastLegoSubmitObj.programId = WrapSys.Config.fragmentPid;
                    WrapSys.Config.lastLegoSubmitObj.hasSubmit = false;
                    dealRoute = 1;
                } else if (WrapSys.Config.lastLegoSubmitObj.hasSubmit == false) {
                    dealRoute = 2  // 如果 尚未提交过;
                } else {
                    dealRoute = 3 //  分片提交过;
                }
                if ((buildNewPA.videotype == 0 || buildNewPA.videotype == 1)) {
                    wrapDelayRoute = true;
                }
                if (WrapSys.Config.FIRST_FRAGMENT_START == null) {
                    WrapSys.Config.FIRST_FRAGMENT_START = WrapSys.Util.formatDateTimeFormat(new Date());
                }
                if (dealRoute == 1 || dealRoute == 2) {
                    var sendData = {"request": JSON.stringify({url: WrapSys.Constant.thirdUrl_peditor_submit, type: "post", params: {
                        data: JSON.stringify(dataJson)
                    }, needCookie: true}), timeSign: new Date().getTime()}

                    if (wrapDelayRoute) {
                        WrapSys.Util.episodeLogSubmit(p_vid, WrapSys.Config.VOD_LOG_TYPE.delaySendDataToLego, sendData, {code: "A00000", data: {programId: p_vid}}, "fromUrl:" + WrapSys.Constant.thirdUrl_peditor_submit);
                        WrapSys.Config.fragmentPid = p_vid;
                        WrapSys.Config.lastLegoSubmitObj.programId = WrapSys.Config.fragmentPid;
                        WrapSys.Config.lastLegoSubmitObj.hasSubmit = true;
                        WrapSys.Config.HAS_SEND_RESET = false;
                        //提交数据给刘之红
                        postFragmentData(node.settings.subData, buildNewPA, node.tempNode.pic, WrapSys.Config.fragmentPid, node.handler)
                    } else {
                        WrapSys.Dao.dopeditorSubmit(sendData, {
                            doSuc: function (res) {
                                if (res == null || res.code != 'A00000') {
                                    WrapSys.Util.logSubmit(WrapSys.Config.VOD_LOG_TYPE.sendProgramDataToLego, sendData, res, "fromUrl:" + WrapSys.Constant.thirdUrl_peditor_submit);
                                    WrapSys.Plugin.tips("error", res == null ? "lego接口无效返回值" : "lego错误：" + res.msg);
                                    node.handler.doRevert();
                                } else {
                                    var programId = res.data.programId;
                                    if (!programId) {
                                        WrapSys.Util.logSubmit(WrapSys.Config.VOD_LOG_TYPE.sendProgramDataToLego, sendData, {error: '乐高返回的programId为空!'}, "fromUrl:" + WrapSys.Constant.thirdUrl_peditor_submit);
                                        node.handler.doError('乐高返回的programId为空!');
                                        return;
                                    }
                                    WrapSys.Util.episodeLogSubmit(programId, WrapSys.Config.VOD_LOG_TYPE.sendProgramDataToLego, sendData, res, "fromUrl:" + WrapSys.Constant.thirdUrl_peditor_submit);
                                    WrapSys.Config.fragmentPid = programId;
                                    WrapSys.Config.lastLegoSubmitObj.programId = WrapSys.Config.fragmentPid;
                                    WrapSys.Config.lastLegoSubmitObj.hasSubmit = true;
                                    WrapSys.Config.HAS_SEND_RESET = false;
                                    //提交数据给刘之红
                                    postFragmentData(node.settings.subData, buildNewPA, node.tempNode.pic, WrapSys.Config.fragmentPid, node.handler)
                                }
                            },
                            doError: function (XMLHttpRequest, status) {
                                WrapSys.Util.logSubmit(WrapSys.Config.VOD_LOG_TYPE.sendProgramDataToLego, sendData, { type: "error reqeust", status: status }, "fromUrl:" + WrapSys.Constant.thirdUrl_peditor_submit);
                                node.handler.doError('网络错误,请稍后再提交！');
                                return;
                            }
                        })
                    }
                } else if (dealRoute == 3) {
                    //提交数据给刘之红
                    postFragmentData(node.settings.subData, buildNewPA, node.tempNode.pic, WrapSys.Config.fragmentPid, node.handler)
                }
                return true;
            }

            function doProcess6() {
                var tmpArr = WrapSys.MaskManager.getNegativeOutpointList(node.tempNode.eTime);
                if (tmpArr.length > 0) {
                    SystemDialog.queryConfirm("[遮标" + tmpArr.join(",") +"]"+ WrapSys.Config.REMIND_MSG_INFO.NO_SET_OUTPOINT_INFO, function (flag) {
                        if (flag) {
                            WrapSys.Fragment.keepOnSubmit();
                        } else {
                            node.handler.doError("WrapSys.Fragment stop in doProcess6", true)
                        }
                    })
                    return false;
                }
                return true;
            }

            function doProcess7() {
                var tmpArr = WrapSys.CornerManager.getNegativeOutpointList(node.tempNode.eTime);
                if (tmpArr.length > 0) {
                    SystemDialog.queryConfirm("[贴片 " + tmpArr.join(",")+"]" + WrapSys.Config.REMIND_MSG_INFO.NO_SET_OUTPOINT_INFO, function (flag) {
                        if (flag) {
                            WrapSys.Fragment.keepOnSubmit();
                        } else {
                            node.handler.doError("WrapSys.Fragment stop in doProcess7", true)
                        }
                    })
                    return false;
                }
                return true;
            }

            function keepOnSubmit() {
                if (node != null) {
                    while (node.processArr.length > 0) {
                        var func = node.processArr.shift();
                        if (!func()) {
                            break;
                        }
                    }
                }
            }

            function doSubmit(parentDiv, callhandler) {
                node = createBaseNode(parentDiv, callhandler);
                while (node.processArr.length > 0) {
                    var func = node.processArr.shift();
                    if (!func()) {
                        break;
                    }
                }
            }

            function transToFragmentMode() {
                $('input[name=videotype]').attr("disabled", "disabled");
                $('input[name=sTime]').attr("readonly", true);
                $('input[name=categoryName]').attr("disabled", "disabled");
                $('input[name=videoName]').attr("readonly", true);
                $('input[name=albumName]').attr("readonly", true);
                $('input[name=location]').attr("readonly", true);
                $("#cp").attr("readonly", true);

                $('input[name=priority]').attr("disabled", "disabled")
                $('input[name=video_dpi]').attr("disabled", "disabled")
                $('input[name=isVolumeStandard]').attr("disabled", "disabled");
                $('input[name=isVolumeStandardForMaterial]').attr("disabled", "disabled");
                $('input[name=needRemakeVideo]').attr("disabled", "disabled");
                $('input[name=reTranscodeConfirm]').attr("disabled", "disabled");
                $("#div_fragment_append").html("");
                $("#div_fragment_append").show();
                WrapSys.Util.handlerChangeFgRemindStart()
                WrapSys.Util.addToDivFragment(1);
            }

            function transToNormalMode() {
                $('input[name=videotype]').removeAttr("disabled");
                $('input[name=sTime]').removeAttr("readonly");
                $('input[name=videoName]').removeAttr("readonly");
                $('input[name=albumName]').removeAttr("readonly");
                $('input[name=location]').removeAttr("readonly");
                $("#cp").removeAttr("readonly");
                $('input[name=priority]').removeAttr("disabled");
                $('input[name=video_dpi]').removeAttr("disabled");
                $('input[name=isVolumeStandard]').removeAttr("disabled");
                $('input[name=isVolumeStandardForMaterial]').removeAttr("disabled");
                $('input[name=needRemakeVideo]').removeAttr("disabled");
                $('input[name=isAppendFragment]').removeAttr("disabled");
                $('input[name=reTranscodeConfirm]').removeAttr("disabled");
                var videotype = $('input[name=videotype]:checked').val();
                if (videotype == -1) {
                    $('input[name=categoryName]').removeAttr("disabled");
                }
                $("#div_fragment_append").hide();
            }

            function postFragmentData(list, newPA, imageId, programId, callhandler) {
                var dotData = WrapSys.Dao.getDotData();
                var pData = WrapSys.Util.makeUpPostData(list, newPA, imageId, programId);
                pData.tag = 0;
                WrapSys.Dao.postSync(dotData.wrapSubmit, pData, {
                    doSuc: function (data) {
                        console.log(" 日志:" + JSON.stringify(pData))
                        WrapSys.Util.episodeLogSubmit(programId, WrapSys.Config.VOD_LOG_TYPE.submitFragmentData, pData, data, "url:" + dotData.wrapSubmit);
                        if (data.error != 0) {
                            callhandler.doError(data.error);
                        } else {
                            callhandler.doSuc();
                            WrapSys.CornerManager.doAccountRowRange();
                            WrapSys.Plugin.tips("success", '提交分片成功');
                        }
                    },
                    doError: function () {
                        WrapSys.Util.episodeLogSubmit(programId, WrapSys.Config.VOD_LOG_TYPE.submitFragmentData, pData, {type: "request error"}, "url:" + dotData.wrapSubmit);
                        callhandler.doError('编码服务器网络错误,请稍后再提交！');
                    }
                }, false)
            }


            return {
                doSubmit: doSubmit,
                transToFragmentMode: transToFragmentMode,
                transToNormalMode: transToNormalMode,
                keepOnSubmit: keepOnSubmit
            }
        })();
    }
);